<?php
/**
 *  
 *
  */
namespace Org\Util;
class Rtwbg {
      public function twbg_balance() {
		  $day=date("Y-m-d");
		  $day_time=strtotime($day);
		  $insert_data=array();
		  $lm=array();
		  $n_time=time(); 
		  $condition['c_id']=22;	
		  
		  
		  //$condition['add_time'] = array(array('gt',$day_time)) ; 
		  $max_c_no=M('lottery_results')->where($condition)->max('c_no'); //获取当天最大期数
		  
		   
		  $url='http://a.apiplus.net/newly.do?token=5ee5aca63461f954&code=twbingo&rows=1&format=json'; //接口地址
		  
		  $new_result=get_http($url);
		  S('TWBG_result',$new_result);
		  $result=json_decode($new_result,true);
		  // print_r($new_result);exit;
	  //台湾宾果 
	   if($max_c_no!=$result['data'][0]['expect']){
		
		  $newa = substr($result['data'][0]['opencode'],0,strrpos($result['data'][0]['opencode'],'+')); 
		  
		  $num= explode(",",$newa);;
		   
		   //print_r($num);exit;
		  $insert_data['c_no']=$result['data'][0]['expect'];
		  $insert_data['c_id']=22;
		  $insert_data['c_name']='台湾宾果';
		  $insert_data['add_time']=$result['data'][0]['opentimestamp'];
		  $insert_data['lottery_result']=$newa;
		  $insert_data['status']=0;
		  S('twbg_'.$result['data'][0]['expect'],$newa);  //存入缓存  方便计算两面长龙算法
		  $sum=array_sum($num);
		  $lm[0]=$this->zh_dx($sum);
		  $lm[1]=$this->zh_ds($sum); 
		  $lm[2]=$this->zh_dxds($sum);
		  $lm[3]=$this->qh($num); 
		  $lm[4]=$this->ds($num);
		  $lm[5]=$this->wx($sum);
		  $insert_data['lm']='';
		// print_r($lm);exit;
		  if($max_c_no+1==$result['data'][0]['expect']){  //计算两面长龙结果
			
			 $flag=1;
			 $flag_zhdx=0;
			 $flag_zhds=0;
			 $flag_zhdxds=0;
			 $flag_qh=0;
			 $flag_ds=0;
			 $flag_wx=0;
			 $k=1;
			 do{
				 
				 $old_lottery=$result['data'][0]['expect']-$k;
				 
				 $twbg_result=S('twbg_'.$old_lottery);
   
				  if(empty($twbg_result))
				  {
				  $condition1['c_id']=22;
				  $condition1['c_no']=$old_lottery;
				  $twbg_result=M('lottery_results')->where($condition1)->getField('lottery_result');
				  if($twbg_result){
				  S('twbg_'.$old_lottery,$twbg_result);
				  }else{
					  $flag=0;
					  }
				  }	
				
				 $num1[$k-1]= explode(",",$twbg_result);
				 $sum1[$k-1]=array_sum($num1[$k-1]);
				 
				   //计算总和大小两面长龙
				 $dx1=$this->zh_dx($sum1[$k-1]);
				 if($flag_zhdx==0){
					
					if($dx1==$lm[0]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 ZHT_810
					   if($dx1=='小'){
					   $zh_dx='ZDX=X,'.$b.',总和小';
					   }elseif($dx1=='大'){
					   $zh_dx='ZDX=D,'.$b.',总和大';
						   }else{
					   $zh_dx='ZHT=T,'.$b.',总和810';		 
							   }
					 }else{
					 $flag_zhdx=1;	 
						 }
				 }
				// print_r($zh_dx);exit;
				//计算总和单双两面长龙
				 $ds1=$this->zh_ds($sum1[$k-1]);
				 if($flag_zhds==0){
					if($ds1==$lm[1]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 
					   if($ds1=='双'){
					   $zh_ds='ZDS=S,'.$b.',总和双';
					   }else{
					   $zh_ds='ZDS=D,'.$b.',总和单';
						   } 
					 }else{
					 $flag_zhds=1;	 
						 }
				 }  
			  // $lm[2]=$this->zh_dxds($sum1);  
			   //计算总和单双两面长龙
				 $dxds1=$this->zh_dxds($sum1[$k-1]);
				 if($flag_zhdxds==0){
					if($dxds1==$lm[2]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 
					   if($dxds1=='大双'){
					   $zh_dxds='DXDS=DS,'.$b.',总大双';
					   }elseif($dxds1=='大单'){
					   $zh_dxds='DXDS=DD,'.$b.',总大单';
						   }elseif($dxds1=='小单'){
					   $zh_dxds='DXDS=XD,'.$b.',总小单';
						   }elseif($dxds1=='小双'){
					   $zh_dxds='DXDS=XS,'.$b.',总小双';
						   } 
					 }else{
					 $flag_zhdxds=1;	 
						 }
				 }   
			   //WX=3,2,火;QHH=H,3,后(多);DSH=D,2,单(多);ZDX=D,3,总和大; 
				//$lm[3]=$this->qh($num);
				$qh1=$this->qh($num1[$k-1]);
				 if($flag_qh==0){
					if($qh1==$lm[3]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 
					   if($qh1=='前'){
					   $qhh='QHH=Q,'.$b.',前(多)';
					   }elseif($qh1=='后'){
					   $qhh='QHH=H,'.$b.',后(多)';
						   }elseif($qh1=='和'){
					   $qhh='QHH=T,'.$b.',前后(和)';
						   }  
					 }else{
					 $flag_qh=1;	 
						 }
				 }
				 //$lm[4]=$this->ds($num);
				$ds1=$this->ds($num1[$k-1]);
				 if($flag_ds==0){
					if($ds1==$lm[4]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 
					   if($ds1=='单'){
					   $dsh='DSH=D,'.$b.',单(多)';
					   }elseif($ds1=='双'){
					   $dsh='DSH=S,'.$b.',双(多)';
						   }elseif($ds1=='和'){
					   $dsh='DSH=T,'.$b.',单双(和)';
						   }  
					 }else{
					 $flag_ds=1;	 
						 }
				 }
				 
				 //$lm[5]=$this->wx($sum1);
				$wx1=$this->wx($sum1[$k-1]);
				 if($flag_wx==0){
					if($wx1==$lm[5]){
					 $b=$k+1;
					 //ZDS=D,2,总和单 
					   if($wx1=='金'){
					   $wx='WX=0,'.$b.',金';
					   }elseif($wx1=='木'){
					   $wx='WX=1,'.$b.',木';
						   }elseif($wx1=='水'){
					   $wx='WX=2,'.$b.',水';
						   }elseif($wx1=='火'){
					   $wx='WX=3,'.$b.',火';
						   }elseif($wx1=='土'){
					   $wx='WX=4,'.$b.',土';
						   }   
						   
							
					 }else{
					 $flag_wx=1;	 
						 }
				 }  
				 $k++;
				if($flag_zhdx==1&&$flag_zhds==1&&$flag_zhdxds==1&&$flag_qh==1&&$flag_ds==1&&$flag_wx==1||$k>10){
				  // if($k<11){
					 $flag=0;
					 echo $flag;
					 }
				   
			 }while($flag==1);
			
			
			 if($zh_dx){	 
			 $s[]=$zh_dx;	
			 }
			 if($zh_ds){	 
			 $s[]=$zh_ds;	
			 } 
			 if($zh_dxds){	 
			 $s[]=$zh_dxds;	
			 }
			 if($qhh){	 
			 $s[]=$qhh;	
			 }
			 if($dsh){	 
			 $s[]=$dsh;	
			 }
			 if($wx){	 
			 $s[]=$wx;	
			 }
		   // print_r($s);exit;
			 $n_s=implode(";",$s);	
		   // print_r($n_s);exit;
			$insert_data['detail']=$n_s;	
			  }
			  
		  $insert_result=M('lottery_results')->data($insert_data)->add();	
		  if(!$insert_result){
			   file_put_contents("test.txt", date("Y-m-d H:i:s") . "获取台湾宾果结果存入失败！" . "\r\n<br>", FILE_APPEND);  	
				  }else{
			  $twbg_result = new \Org\Util\Jstwbg();  //台湾宾果
			$twbg_result->twbg_balance();
		  }
		  }
		  
		
		  
		  
		  
	
		
		
					 
		return; 		
     }
	
 public function zh_ds($sum){  //总和单双
      if($sum%2==0){
		$ds='双';
			}else{
		$ds='单';	 
		}
		return $ds;
 }
 public function zh_dx($sum){  //总和大小
      if($sum>810){
		   $dx='大';	 
			 }elseif($sum<810){
			 $dx='小';	 
				 }else{
				$dx='和';	 
					 }
		return $dx;
 }
 public function zh_dxds($sum){
	 if($sum%2==0&&$sum>810){
		$dx='大双'; 
		 }elseif($sum%2==0&&$sum<810){
		$dx='小双'; 	 
			 }elseif($sum%2!=0&&$sum<810){
		$dx='小单'; 	 
			 }elseif($sum%2!=0&&$sum>810){
		$dx='大单'; 	 
			 }else{
		$dx='和局';		 
				 }
	return $dx;			 
	 } 
 public function qh($num){
	  $a=1;
	  $b=1;
	  foreach($num as $k=>$v){
		  if($v<=40){
			  $a=$a+1;
			  }else{
				$b=$b+1;  
				  }
		  }
	  if($a>$b){
		  $dx='前';
		  }elseif($a<$b){
			$dx='后';  
			  }else{
			$dx='和';  	  
				  }	  
	 return $dx;
	 }
 public function ds($num){
	  $a=1;
	  $b=1;
	  foreach($num as $k=>$v){
		  if($v%2==0){
			  $a=$a+1;
			  }else{
				$b=$b+1;  
				  }
		  }
	  if($a>$b){
		  $dx='双';
		  }elseif($a<$b){
			$dx='单';  
			  }else{
			$dx='和';  	  
				  }	  
	 return $dx;
	 }	 
  public function wx($sum){
	  
	  if($sum<=695){
		  $dx='金';
		  }elseif($sum<=763&&$sum>=696){
			$dx='木';  
			  }elseif($sum<=855&&$sum>=764){
			$dx='水';  
			  }elseif($sum<=923&&$sum>=856){
			$dx='火';  
			  }elseif($sum<=1410&&$sum>=924){
			$dx='土';  
			  }
	  return $dx;
	  }	
//PC蛋蛋函数
 public function pc_ds($sum){  //总和单双
      if($sum%2==0){
		$ds='双';
			}else{
		$ds='单';	 
		}
		return $ds;
 }
 public function pc_dx($sum){  //总和大小
      if($sum>=14){
		   $dx='大';	 
			 }else{
			 $dx='小';	 
				 }
		return $dx;
 }
 public function pc_jdx($sum){  //总和极大小
      $dx='无';
      if($sum>=22&&$sum<=27){
		   $dx='极大';	 
			 }
	  if($sum>=0&&$sum<=5){
			 $dx='极小';	 
			 }
		return $dx;
 }
 public function pc_dxds($sum){
	 if($sum%2==0&&$sum>=14){
		$dx='大双'; 
		 }elseif($sum%2==0&&$sum<14){
		$dx='小双'; 	 
			 }elseif($sum%2!=0&&$sum<14){
		$dx='小单'; 	 
			 }elseif($sum%2!=0&&$sum>13){
		$dx='大单'; 	 
			 } 
	return $dx;			 
	 }	 
//PC波色
 public function pc_bs($sum){  //波色
 
 /*• 绿波号码：1 , 4 , 7 , 10 , 16 , 19 , 22 , 25
• 蓝波号码：2 , 5 , 8 , 11 , 17 , 20 , 23 , 26
• 红波号码：3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 */
  $red=array('3','6','9','12','15','18','21','24');
  $green=array('1','4','7','10','16','19','22','25');
  $blue=array('2','5','8','11','17','20','23','26');
  $ds='无';
   if (in_array($sum, $red)){
	 $ds='红';  
	   } 
	if (in_array($sum, $green)){
	 $ds='绿';  
	   } 
	if (in_array($sum, $blue)){
	 $ds='蓝';  
	   }        
	return $ds;
 }
 
 public function pc_bz($num){  //豹子
 
 /*• 绿波号码：1 , 4 , 7 , 10 , 16 , 19 , 22 , 25
• 蓝波号码：2 , 5 , 8 , 11 , 17 , 20 , 23 , 26
• 红波号码：3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 */
   $ds='无';
   if ($num[0]==$num[1]&&$num[0]==$num[2]){
	 $ds='豹子';  
	   } 
       
	return $ds;
 }	   
	 
}